package com.efast.dmom.aps.datahandlers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.PartInventory;
import com.efast.dmom.aps.dao.PartInventoryDao;
import com.efast.dmom.aps.entity.PartInventoryEntity;
import com.efast.dmom.aps.util.TimeUtil;

@Component
public class PartInventoryDataHandler {
	
	@Autowired
	private PartInventoryDao partInventoryDao;
	
	public Map<String, Integer> getPartInventory() {
		List<PartInventoryEntity> list = partInventoryDao.getAllPartInventory();
		Map<String,Integer> map = new HashMap<String, Integer>();
		if(list != null && list.size() > 0) {
			for(PartInventoryEntity entityData:list) {
				map.put(entityData.getPartNo()+"-"+entityData.getPartRevision(), (int) entityData.getQty());
			}
		}
		return map;
	}

	public PartInventory getPartInventoryByPartId(long partId) {
		PartInventoryEntity partInventoryEntity = partInventoryDao.getPartInventoryByPartId(partId);
		PartInventory partInventory = new PartInventory();
		partInventory.setInnerData(partInventoryEntity);
		return partInventory;
	}

	public PartInventory getByName(String name) {
		PartInventoryEntity partInventoryEntity = partInventoryDao.getByName(name);
		PartInventory partInventory = new PartInventory();
		partInventory.setInnerData(partInventoryEntity);
		return partInventory;
	}

	public void save(PartInventory obj) {
		if(obj.getInnerData() != null) {
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
		obj.getInnerData().setCreationPid(1);
		obj.getInnerData().setLastModifiedPid(1);
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		partInventoryDao.save(obj.getInnerData());
		}
	}

	public PartInventory getById(long id) {
		PartInventoryEntity partInventoryEntity = partInventoryDao.getById(id);
		PartInventory partInventory = new PartInventory();
		partInventory.setInnerData(partInventoryEntity);
		return partInventory;
	}

	public void update(PartInventory obj) {
		if(obj.getInnerData() != null){
			
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifiedPid(1);	
			partInventoryDao.update(obj.getInnerData());
		}
		
	}

	public void delete(PartInventory obj) {
		if(obj.getInnerData() != null){
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifiedPid(1);			
			partInventoryDao.delete(obj.getInnerData());
		}
		
	}

}
